package com.yanqingshan.platform.module.sys.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.lang.tree.Tree;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.yanqingshan.platform.common.constant.AppConstant;
import com.yanqingshan.platform.common.constant.CommonConstant;
import com.yanqingshan.platform.common.core.domain.R;
import com.yanqingshan.platform.module.sys.model.domain.SysResource;
import com.yanqingshan.platform.module.sys.model.vo.SysResourceRespVO;
import com.yanqingshan.platform.module.sys.service.SysResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 功能描述: 系统菜单前端控制器
 *
 * @author yanqs
 * @since 2023-05-15
 */
@Api(tags = "系统资源")
@ApiSupport(order = 4)
@RestController
@SaIgnore
@RequestMapping(AppConstant.APPLICATION_SYSTEM + "/resource")
public class SysResourceController {

    @Resource
    private SysResourceService resourceService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("创建菜单")
    @PostMapping("/create")
    public R create(@RequestBody SysResource resource) {
        if (Objects.isNull(resource.getParentId())) {
            resource.setParentId(CommonConstant.ROOT_ID);
        }
        return R.status(resourceService.save(resource));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("更新菜单")
    @PutMapping("/update")
    public R update(@RequestBody SysResource resource) {
        if (Objects.isNull(resource.getParentId())) {
            resource.setParentId(CommonConstant.ROOT_ID);
        }
        return R.status(resourceService.updateById(resource));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("删除菜单")
    @DeleteMapping("/{id}")
    public R delRole(@PathVariable Long id) {
        return R.status(resourceService.removeById(id));
    }


    @ApiOperationSupport(order = 4)
    @ApiOperation("校验资源别名唯一性")
    @GetMapping("/validate-resource-key")
    public R<Long> validateRoleKey(@RequestParam String resourceKey, @RequestParam Long id) {
        long cnt = resourceService.validateResourceKey(resourceKey, id);
        return R.ok(cnt);
    }


    @ApiOperationSupport(order = 5)
    @ApiOperation("获取菜单(管理模块)")
    @GetMapping("/list-for-manage")
    public R<List<Tree<Long>>> listForManage() {
        List<Tree<Long>> treeNodes = resourceService.listForManage();
        return R.ok(treeNodes);
    }

    @ApiOperation("角色授权")
    @GetMapping(value = "/list-for-grant")
    public R<SysResourceRespVO> listForGrant(@RequestParam Long roleId) {
        return R.ok(resourceService.listForGrant(roleId));
    }

    @ApiOperation("根据用户编号查询资源权限列表")
    @GetMapping(value = "/list-all-resources/{userId}")
    public R listAllResourcesByUserId(@PathVariable Long userId){
        return R.ok(resourceService.listAllResourcesByUserId(userId));
    }
}
